package com.zeei.ems.meet.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zeei.ems.base.page.PageParamVO;
import com.zeei.ems.meet.dao.MeetingContactDao;
import com.zeei.ems.meet.model.MeetingContactVO;
import com.zeei.ems.meet.model.vo.MeetingContactParam;
import com.zeei.ems.meet.service.MeetingContactService;
import com.zeei.ems.meet.util.MeetingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class MeetingContactServiceImpl implements MeetingContactService, CommandLineRunner {
    @Autowired
    MeetingContactDao meetingContactDao;

    @Override
    public void run(String... args) throws Exception {
        MeetingContactParam meetingContact = new MeetingContactParam();
        List<MeetingContactVO> meetingContactList = meetingContactDao.listMeetingContact(meetingContact);
        if (!CollectionUtils.isEmpty(meetingContactList)){
            MeetingUtil.createUser(meetingContactList);
        }
    }

    @Override
    public PageInfo<MeetingContactVO> listMeetingContact(PageParamVO page, MeetingContactParam meetingContact) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<MeetingContactVO> meetingContactList = meetingContactDao.listMeetingContact(meetingContact);
        return new PageInfo<>(meetingContactList);
    }

    @Override
    public List<MeetingContactVO> listMeetingContact(MeetingContactParam meetingContact) {
        return meetingContactDao.listMeetingContact(meetingContact);
    }

    @Override
    public Boolean addMeetingContact(MeetingContactVO meetingContact) {
        meetingContact.setUserStatus(0);
        Integer integer = meetingContactDao.addMeetingContact(meetingContact);
        if (integer > 0) {
            MeetingUtil.createUser(meetingContact);
            return Boolean.TRUE;
        }

        return Boolean.FALSE;
    }

    @Override
    public Boolean updateMeetingContact(MeetingContactVO meetingContact) {
        Integer integer = meetingContactDao.updateMeetingContact(meetingContact);
        if (integer > 0) {
            MeetingUtil.createUser(meetingContact);
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }

    @Override
    public Boolean deleteMeetingContact(List<Integer> userIdList) {
        List<MeetingContactVO> meetingContactList = meetingContactDao.getMeetingContactList(userIdList);
        if (!CollectionUtils.isEmpty(meetingContactList)){
            List<Integer> userIds = new ArrayList<>();
            meetingContactList.forEach(item -> {
                userIds.add(item.getUserId());
            });

            Integer integer = meetingContactDao.deleteMeetingContact(userIdList);
            if (integer > 0) {
                MeetingUtil.deleteUser(meetingContactList);
                return Boolean.TRUE;
            }
        }

        return Boolean.FALSE;
    }
}
